import qs from 'qs'
import urlOptions from '../../urladress.js'
import $ from 'jquery'
export default {
    name: "Devicesetdetail",
    components: {},
    urlOptions,
    props: ['listData', "item"],
    data() {
        return {
            deviceId: this.$route.params.deviceId,
            detailList: [],
            handleDialog: false,
            formLabelWidth: '120px',
            form: {
                value: ""
            },
            deviceName: "",
            dataName: "",
            vhtml: "",
            arr: "",
            websock: null,
            objectId: "",
            objectName: "",
            value: "",
        };
    },
    computed: {},
    watch: {
        handleDialog: function (newVal, oldVal) {
            if (oldVal) {
                this.form = {
                    value: ""
                }
            }
        }
    },
    created() {
        console.log(this.deviceId);
        this.initList();
        //this.initWebSocket();
    },
    methods: {
        //初始化websocket
        initWebSocket() {
            var urladress = JSON.stringify(urlOptions);
            var urladress2 = JSON.parse(urladress);
            console.log(urladress2);
            var users = JSON.parse(window.localStorage.getItem('userInfo'));
            var userID = users.uId;
            console.log(userID);
            const wsuri = urladress2.urladress + userID;//本地地址
            console.log(wsuri);
            this.websock = new WebSocket(wsuri);
            this.websock.onopen = this.websocketonopen;//连接建立之后执行send方法发送数据 websocketsend(Data)//数据发送
            this.websock.onmessage = this.websocketonmessage;//数据接收
            this.websock.onerror = this.websocketonerror;//连接建立失败重连
            this.websock.onclose = this.websocketclose;//直接关闭
        },
        websocketonopen() {
            console.log("WebSocket连接成功");
            var users = JSON.parse(window.localStorage.getItem('userInfo'));
            var userID = users.uId;
            //console.log("当前用户ID", userID);
            //console.log("设备objectId", this.objectId);
            //console.log("设备的名称", this.objectName);
            //console.log("设备id", this.deviceId);
            let actions = "read:" + this.deviceId + "," + this.objectId + "," + this.objectName + "," + userID;
            //console.log(actions);
            //发送数据
            this.websocketsend(actions);
            //接受数据
            //this.websocketonmessage();
        },
        websocketonmessage(e) { //数据接收 
            console.log("数据接收", e);
            const data = JSON.parse(e.data);
            var valueName = data.value;
            var name = this.objectId;
            //console.log("resourceId", name);
            //console.log("返回resourceId",valueName[0].resourceId);
            $('.Wrapcotent').find('ul').each(function() {
                var resourceId = $(this).find("li:eq(1)").text();
                //console.log("遍历的resorceId",resourceId);
                if(resourceId == valueName[0].resourceId){
                    $(this).find("li:eq(3)").text(valueName[0].value)
                }
            });
        },
        websocketonerror(e) { //连接发生错误的时候重新连接
            this.initWebSocket();
        },
        websocketsend(data) { //数据发送 
            console.log("数据发送", data);
            this.websock.send(data);
        },
        websocketclose(e) { //关闭 
            console.log("断开连接", e);
        },
        initList() {
            this.detailList = this.listData;
        },
        readBtn(item) {
            //读取数据
            this.objectName = item.name;
            this.objectId = item.id;
            this.initWebSocket();
        },
        writerBtn(item) {
            this.objectId = item.id;
            console.log(this.objectId);
            this.deviceName = item.name;
            console.log(this.deviceName);
            this.handleDialog = true;
        },
        //写入
        sureBtn() {
            var postData = {
                id: this.objectId,
                value: this.form.value,
                rowkey: this.item.rowKey,
            }
            console.log("确定", postData);
            this.$http.post('/deviceCommonInfoController/updateDeviceInfoAttribute', qs.stringify(postData))
                .then((res) => {
                    if (res.status == 200) {
                        this.$message({
                            type: "success",
                            message: "写入成功",
                        });
                        this.handleDialog = false;
                        return;
                    }
                }).catch((err) => {
                    console.log(err);
                });
        },
        destroyed() {
            this.websock.close() //离开路由之后断开websocket连接
        },
    }
};